<?php
use OC\Files\Cache\Cache;

JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_puser/tables');
JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_jobs/tables');
JLoader::register('PCompanyModelSubAccounts', OC::$SERVERROOT."/apps/company/lib/subaccounts.php");
/**
 * Search Resumes controller
 */
class PResumesModelSearchResume extends PResumesModelResume
{
   public function getSearchResultByID($id,$user_id) {
      if (empty($id))
         return array();

      $db = PFactory::getDbo();

      $query = "SELECT * FROM *PREFIX*hr_search_jobseeker WHERE id = ".intval($id)." AND user_id = ".intval($user_id);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      return $db->loadObject();
   }

   public function updateResumeIsViewed($info,$user_id) {
      if (!isset($info->source) || !isset($info->resume_code))
         return false;

      $db = PFactory::getDbo();

      $query = "UPDATE *PREFIX*hr_search_jobseeker SET is_viewed = 1 WHERE user_id = ".intval($user_id)." AND source = ".$db->quote($info->source)." AND resume_code = ".$db->quote($info->resume_code);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      $db->execute();

      // Clear Cache
      CacheHelper::triggerRemove($user_id, array('*PREFIX*hr_search_jobseeker'));
      return true;
   }

   public function hide($is_show=0) {
      $resumecodes = json_decode(JRequest::getString("resumecodes", ""));
      $source = JRequest::getString('source', '');

      if(empty($resumecodes) || empty($source)) {
         $this->setError("您无权访问该资源");
         return false;
      }

      $db = PFactory::getDbo();
      $user_id = PFactory::getMasterUser()->get('id');

      $query = "UPDATE *PREFIX*hr_search_jobseeker SET is_show = ".intval($is_show)." WHERE user_id = ".intval($user_id)." AND source = ".$db->quote($source)." AND resume_code IN ('".implode("','", $resumecodes)."')";
      $db->setQuery(PUtil::replaceOCPrefix($query));
      if(!$db->execute()) {
         $msg = $is_show ? '取消隐藏失败!' : '隐藏失败!';
         $this->setError($msg);
         return false;
      }

      // Event Driven Cache/Object Caching - clear any cache related to affected tables
      CacheHelper::triggerRemove($user_id, array('*PREFIX*hr_search_jobseeker'));

      return true;
   }

   public function getSearchResultByCode($user_id, $code, $source) {
      if (empty($user_id) || empty($code) || empty($source))
         return array();
   
      $db = PFactory::getDbo();
   
      $query = "SELECT * FROM *PREFIX*hr_search_jobseeker WHERE user_id = ".intval($user_id)." AND resume_code = ".$db->quote($code) ." AND source = ".$db->quote($source);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      return $db->loadAssoc();
   }
}
